//! 提供读取数据 trait

use std::fmt::Display;

use crate::{App, AppData, FQLType};

pub trait Reader {
    fn index(&self, index: usize) -> Option<&AppData>;

    fn find<T: Display>(&self, key: T, val: FQLType) -> Option<&AppData>;

    fn find_index<T: Display>(&self, key: T, val: FQLType) -> Option<usize>;

    fn find_index_from_data(&self, data: &AppData) -> Option<usize>;

    fn find_all<T: Display>(&self, key: T, val: FQLType) -> Vec<&AppData>;

    fn find_all_index<T: Display>(&self, key: T, val: FQLType) -> Vec<usize>;

    fn find_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<&AppData>;
    fn find_index_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<usize>;
}

impl Reader for App {
    /// 查找指定序号的数据
    fn index(&self, index: usize) -> Option<&AppData> {
        self.data.get(index)
    }

    /// 查找第一个符合条件的数据
    fn find<T: Display>(&self, key: T, val: FQLType) -> Option<&AppData> {
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    return Some(item);
                }
            }
        }

        None
    }

    /// 查找第一个符合条件的数据的序号
    fn find_index<T: Display>(&self, key: T, val: FQLType) -> Option<usize> {
        let mut index = 0;
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    return Some(index);
                }
            }

            index += 1;
        }

        None
    }

    /// 获取所有符合条件的数据
    fn find_all<T: Display>(&self, key: T, val: FQLType) -> Vec<&AppData> {
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    res.push(item);
                }
            }
        }

        res
    }

    /// 获取所有符合条件的数据序号
    fn find_all_index<T: Display>(&self, key: T, val: FQLType) -> Vec<usize> {
        let mut index = 0;
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    res.push(index);
                }
            }

            index += 1;
        }
        res
    }

    /// 获取所有符合过滤函数的数据
    fn find_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<&AppData> {
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if f(v) {
                    res.push(item);
                }
            }
        }

        res
    }

    // 获取所有符合过滤函数的数据
    fn find_index_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<usize> {
        let mut index = 0;
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if f(v) {
                    res.push(index);
                }
            }

            index += 1;
        }

        res
    }

    fn find_index_from_data(&self, data: &AppData) -> Option<usize> {
        let mut index = 0;
        for item in &self.data {
            if item.eq(data) {
                return Some(index);
            }

            index += 1;
        }

        None
    }
}
